//
//  ViewController.m
//  UI__UITableViewEdit
//
//  Created by 冀永金 on 16/7/26.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *nameArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <NSIndexPath *>*selectedIndexArray;// 选中下标的数组
@property (nonatomic, strong) NSMutableArray <NSString *>*selectedNameArray;//选中名字的数组
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *arr = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];
    self.selectedIndexArray = [NSMutableArray array];
    self.selectedNameArray = [NSMutableArray array];
    self.nameArr = [NSMutableArray arrayWithArray:arr];
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
    [self.view addSubview:_tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    // tableview编辑
    // 1.设置左barButtonItem 为编辑的editButtonItem
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    NSLog(@"%@",NSStringFromSelector( self.editButtonItem.action));
    self.editButtonItem.action = @selector(handle);
    
    
    
    
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)handle {
    [self performSelector:NSSelectorFromString(@"_toggleEditing:")];
    // 要删除选中的数组
    // 删除数据源
            [_nameArr removeObjectsInArray:_selectedNameArray];
            //删除cell
    
            [_tableView deleteRowsAtIndexPaths:_selectedIndexArray withRowAnimation:UITableViewRowAnimationAutomatic];
           [_selectedNameArray removeAllObjects];
           [_selectedIndexArray removeAllObjects];

}
// 2.让tableView进入编辑装填
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
   [_tableView setEditing:editing animated:animated];
//    if (editing) {
//        NSLog(@"编辑");
//    }else {
//        NSLog(@"完成");
        // 要删除选中的数组
        // 删除数据源
//        [_nameArr removeObjectsInArray:_selectedNameArray];
//        //删除cell
//        
//        [_tableView deleteRowsAtIndexPaths:_selectedIndexArray withRowAnimation:UITableViewRowAnimationAutomatic];
//       [_selectedNameArray removeAllObjects];
//       [_selectedIndexArray removeAllObjects];
//        _selectedIndexArray removeObjectsAtIndexes:
    //}
}
//3. 控制tableview能否进行编辑，YES可以  NO不可以
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}



// 4.返回cell的编辑样式 默认为Delete
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 返回  多选
    return UITableViewCellEditingStyleInsert |UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
        // 在全选状态下，数组中添加对应选中的下标
        [_selectedIndexArray addObject:indexPath];
        [_selectedNameArray addObject:_nameArr[indexPath.row]];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
        // 在全选状态下，数组中移除对应选中的下标
        [_selectedIndexArray removeObject:indexPath];
        [_selectedNameArray removeObject:_nameArr[indexPath.row]];
    }
}

// 5.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
#warning 如果对tableview进行添加、删除  ，先去修改数据源，再去进行对应的添加、删除
        [_nameArr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }else if (editingStyle == UITableViewCellEditingStyleInsert) {
        [self.nameArr insertObject:@"张三" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}
// 能否能移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 更新数据源
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    
    [_nameArr exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.nameArr.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    cell.textLabel.text = [self.nameArr objectAtIndex:indexPath.row];
    return cell;
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
